package top.sdwu_top.kbase.common.aspect;

import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import top.sdwu_top.kbase.common.aspect.exception.MyException;
import top.sdwu_top.kbase.common.model.Wrapper;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@ControllerAdvice
public class GlobalExceptionAspect {

    @ExceptionHandler(value = MyException.class)
    public Wrapper<String> myExceptionHandle(HttpServletRequest request, MyException e) {
        log.info("异常请求路径:{},参数{}",request.getServletPath(), request.getParameterMap());
        log.info("异常代码:{},异常信息:{}", e.getCode(), e.getMessage());
        e.printStackTrace();
        return Wrapper.wrap(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public Wrapper<String> unkownExceptionHandle(HttpServletRequest request, Exception e) {
        log.info("未知异常请求路径:{},参数{}",request.getServletPath(), request.getParameterMap());
        e.printStackTrace();
        return Wrapper.wrap(500, "Internal server error");
    }


}
